package com.example.administrator.School.adpter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.School.R;

import java.util.Vector;

public class AppShowAdapter extends BaseAdapter {
    private int[] image;
    private String[] name;
    private boolean isChice[];
    private Context context;

    public AppShowAdapter(int[] im,String[] name, Context context) {
        this.image = im;
        this.name = name;
        Log.i("hck", im.length+"lenght");
        isChice=new boolean[im.length];
        for (int i = 0; i < im.length; i++) {
            isChice[i]=false;
        }
        this.context = context;
    }

    @Override
    public int getCount() {
        return image.length;
    }

    @Override
    public Object getItem(int arg0) {
        return image[arg0];
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        View view = arg1;
        GetView getView=null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.kaoqin_gridview_item, null);
            getView = new GetView();
            getView.imageView=(ImageView) view.findViewById(R.id.img);
            getView.textView=(TextView) view.findViewById(R.id.text);
            getView.cl1=(ImageView) view.findViewById(R.id.cl2);
            view.setTag(getView);
        } else {
            getView = (GetView) view.getTag();
        }
        getView.imageView.setImageResource(image[arg0]);
        getView.textView.setText(name[arg0]);
        if (isChice[arg0]== true){
            getView.cl1.setVisibility(View.VISIBLE);
        }else{
            getView.cl1.setVisibility(View.GONE);
        }
        return view;
    }

    static class GetView {
        ImageView imageView;
        TextView textView;
        ImageView cl1;
    }


    //主要就是下面的代码了
//    private LayerDrawable getView(int post) {
//
//        Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(image[post])).getBitmap();
//        Bitmap bitmap2=null;
//        LayerDrawable la=null;
//        if (isChice[post]== true){
//            bitmap2 = BitmapFactory.decodeResource(context.getResources(),
//                    R.drawable.click);
//        }
//        if (bitmap2!=null) {
//            Drawable[] array = new Drawable[2];
//            array[0] = new BitmapDrawable(bitmap);
//            array[1] = new BitmapDrawable(bitmap2);
//            la= new LayerDrawable(array);
//            la.setLayerInset(0, 0, 0, 50, 50);   //第几张图离各边的间距
//            la.setLayerInset(1, 0, 65, 65, 0);
//        }
//        else {
//            Drawable[] array = new Drawable[1];
//            array[0] = new BitmapDrawable(bitmap);
//            la= new LayerDrawable(array);
//            la.setLayerInset(0, 20, 20, 0, 0);
//        }
//        return la; // 返回叠加后的图
//    }
    public void chiceState(int post)
    {
        isChice[post]=isChice[post]==true?false:true;
        this.notifyDataSetChanged();
    }
}